<script>
	import {
		mapActions,
		mapMutations
	} from 'vuex'
	import {
		getConfig
	} from '@/api/app'
	import {
		strToParams,
		strToQuery
	} from '@/utils/tools'
	import {
		toLogin
	} from '@/utils/login'
	import {
		bindSuperior
	} from '@/api/user'
	import {INVITE_CODE,INVITE_RANDOM_CODE,INVITE_LEVEL_ID,SID,INVITE_REGISTER,WECHATSCENECODE,DISTRIBUTION,DISTRIBUTIONID,IS_CHANGE_INVITE_CODE} from '@/config/cachekey'
	import Cache from '@/utils/cache'
	export default {
		onLaunch: function(options) {
			// console.log('==/App.vue===onLaunch==checkMpUpdate==getUser==getSystemInfo()=======');
			//#ifdef MP-WEIXIN
			this.checkMpUpdate()
			//#endif
			// 获取配置
			// console.log('app_vue_onLaunch_options',options)
			//this.getUser()
			this.getSystemInfo()
		},
		onShow: function(options) {
			// uni.hideTabBar();
			// console.log('==/App.vue===onShow==从请求路径/二维码/中获取并设置===invite_code=');
			console.log(options)
			
			let scene = options.query.scene ? strToParams(decodeURIComponent(options.query.scene)) : {};	//query.scene中获取内容
			let inviteCode = scene.invite_code || options.query.invite_code || '';
			let inviteCodeCache = Cache.get(INVITE_CODE);
            if (scene) {
                options.id = scene.id;
				scene.d ? this.setDistributionId(scene.d) : this.setDistributionId(scene.id)
				Cache.set(SID, scene.sid)
                Cache.set(INVITE_RANDOM_CODE, scene.r)
                Cache.set(INVITE_LEVEL_ID, scene.l)
            }else if(options.query && options.query.distributionId){
				console.log('==/App.vue===onShow==options.query.invite_code');
                this.setDistributionId(options.query.distributionId)
            }
			//设置 inviteCode
			if(inviteCode){
				if(inviteCode != inviteCodeCache){
					Cache.set(IS_CHANGE_INVITE_CODE, 1);
				}else{
					Cache.set(IS_CHANGE_INVITE_CODE, 0);
				}
				Cache.set(INVITE_CODE, inviteCode)
				Cache.set(INVITE_REGISTER, inviteCode);
				this.setInviteCode(scene.invite_code);
			}
			// console.log('==/App.vue===onShow==WECHATSCENECODE');
			Cache.set(WECHATSCENECODE, this.getWechatSceneCode());
			// console.log('==/App.vue===onShow==getConfigFun===调用接口==/api/index/config===================');
			this.getConfigFun()
			this.bindCode(options)
		},
		onHide: function() {

		},
		methods: {
			...mapActions(['getSystemInfo', 'getUser', 'wxShare', 'initLocationFunc']),
			...mapMutations(['setConfig','setDistributionId','setInviteCode']),
			async getConfigFun() {
				let CacheDistribution = Cache.get(DISTRIBUTION);
				let is_change_invite_code = Cache.get(IS_CHANGE_INVITE_CODE) || 0;
				// console.log('---------is_change_invite_code--------', is_change_invite_code);
				if(!CacheDistribution || is_change_invite_code){
					console.log('==/App.vue==getConfigFun==no==CacheDistribution==toLogin=====----------------------------------------------------------');
					console.log(is_change_invite_code);
					toLogin()
				}else{
					console.log('==/App.vue==getConfigFun==CacheDistribution=no=toLogin=====');
					console.log(CacheDistribution);
				}
				// toLogin()
				const {
					code,
					data
				} = await getConfig()
				this.setConfig(data)
				if (code == 1) {
					//Cache.set(DISTRIBUTIONID, data.distribution.id)
					/* if(data.is_open_nearby) {
						// 获取定位
						this.initLocationFunc()
					} */
					// 防止第一次调用时拿不到数据
					this.wxShare()
				}

			},
			getWechatSceneCode(){
				const launchOptions = uni.getLaunchOptionsSync();
				if(launchOptions.scene){
					return launchOptions.scene;
				}else{
					return '0';
				}
			},
			bindCode(options) {
				/* if(!options.query) return
				Cache.set(INVITE_CODE, options.query.invite_code) */
				/* let invite_code = options.query.invite_code || strToParams(decodeURIComponent(options.query.scene))
					.invite_code
				if (invite_code) {
					const {
						data,
						code
					} = await bindSuperior({
						code: invite_code
					})
					if (code == -1) {
						Cache.set(INVITE_CODE, invite_code)
					}
				} */
			},
			//#ifdef MP-WEIXIN
			checkMpUpdate() {
				const updateManager = wx.getUpdateManager()
				updateManager.onUpdateReady(function () {
				  wx.showModal({
				    title: '更新提示',
				    content: '新版本已准备好，是否重启？',
				    success: function (res) {
				      if (res.confirm) {
				        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
				        updateManager.applyUpdate()
				      }
				    }
				  })
				})
			}
			//#endif
		}
	}
</script>
<style lang="scss">
	@import 'styles/common.scss';
	@import "components/uview-ui/index.scss";
	@import url('@/plugin/emoji-awesome/css/apple.css');
	// 小米字体--由粗到细
	@font-face {  
	    font-family: 'MiSans-Bold'; /* 自定义字体名称 */  
	    src: url('https://joy.ytzzmall.com/font/font/MiSans-Bold.woff2') format('woff2'); /* 字体文件路径和格式 */  
	}
	@font-face {
	    font-family: 'MiSans-Medium'; /* 自定义字体名称 */
	    src: url('https://joy.ytzzmall.com/font/font/MiSans-Medium.woff2') format('woff2'); /* 字体文件路径和格式 */  
	}
	@font-face {
	   font-family: 'MiSans-Regular'; /* 自定义字体名称 */
	   src: url('https://joy.ytzzmall.com/font/font/MiSans-Regular.woff2') format('woff2'); /* 字体文件路径和格式 */  
	}
	@font-face {
	    font-family: 'MiSans-Normal'; /* 自定义字体名称 */
	    src: url('https://joy.ytzzmall.com/font/font/MiSans-Normal.woff2') format('woff2'); /* 字体文件路径和格式 */  
	}
	@font-face {
	    font-family: 'MiSans-Light'; /* 自定义字体名称 */
	    src: url('https://joy.ytzzmall.com/font/MiSans-Light.woff2') format('woff2'); /* 字体文件路径和格式 */  
	}
	@font-face {
	    font-family: 'MiSans-Thin'; /* 自定义字体名称 */
	    src: url('https://joy.ytzzmall.com/font/font/MiSans-Thin.woff2') format('woff2'); /* 字体文件路径和格式 */  
	}
	
	
	
	// 鸿蒙字体, 由粗到细
	@font-face {
	    font-family: 'HOS-Bold'; /* 自定义字体名称 */
	    src: url('https://joy.ytzzmall.com/font/HarmonyOS_Sans_SC_Bold.ttf') format('ttf'); /* 字体文件路径和格式 */  
	}
	@font-face {
	    font-family: 'HOS-Medium'; /* 自定义字体名称 */  
	    src: url('https://joy.ytzzmall.com/font/HarmonyOS_Sans_SC_Medium.ttf') format('ttf'); /* 字体文件路径和格式 */  
	}
	@font-face {
	    font-family: 'HOS-Regular'; /* 自定义字体名称 */  
	    src: url('https://joy.ytzzmall.com/font/HarmonyOS_Sans_SC_Regular.ttf') format('ttf'); /* 字体文件路径和格式 */  
	}
	@font-face {
	   font-family: 'HOS-Light'; /* 自定义字体名称 */
	   src: url('https://joy.ytzzmall.com/font/HarmonyOS_Sans_SC_Light.ttf') format('ttf'); /* 字体文件路径和格式 */  
	}
	@font-face {
	    font-family: 'HOS-Thin'; /* 自定义字体名称 */
	    src: url('https://joy.ytzzmall.com/font/HarmonyOS_Sans_SC_Thin.ttf') format('ttf'); /* 字体文件路径和格式 */  
	}
	
	/* 如果需要，可以添加其他格式的字体文件，以提高兼容性 */
	/* src: url('/static/fonts/mycustomfont.woff2') format('woff2'),  
	     url('/static/fonts/mycustomfont.woff') format('woff'); */  
	
	/*每个页面公共css */
	body, html, .uni-page-body, view, text, page {  
	    // font-family: cursive, sans-serif !important; /* 使用自定义字体，并指定后备字体 */  
		font-family: 'HOS-Light','MiSans-Light', PingFang SC, Arial; /* 使用自定义字体，并指定后备字体 */  
	}
</style>
